import { GetAllCategory, GetHotArticle, GetTopTag } from '@/api/blog-api'

export default {
    namespaced: true,
    state: {
        category: [],
        hotTag: [],
        hotArticle: [],
        socket: null
    },
    mutations: {
        SetCategory (state, { category = [] }) {
            state.category = category
        },
        SetHotTag (state, { hotTag = [] }) {
            state.hotTag = hotTag
        },
        SetHotArticle (state, { hotArticle = [] }) {
            state.hotArticle = hotArticle
        },
        InitSocket (state, { socket = [] }) {
            state.socket = socket
        },
        SendLog (state, { log = {} }) {
            state.socket && state.socket.emit('clientMsg', log)
        }
    },
    actions: {
        /**
         * @description 获取分类信息
         * @author Ryui
         */
        InitCategory ({ commit }) {
            return new Promise((resolve, reject) => {
                GetAllCategory()
                    .then(async res => {
                        // 设置 vuex/localStorage 用户信息
                        commit('SetCategory', { category: res.data })
                        resolve()
                    }).catch(err => {
                    console.log('err: ', err)
                    reject(err)
                })
            })
        },

        /**
         * @description 获取热门标签
         * @author Ryui
         */
        InitHotTag ({ commit }) {
            return new Promise((resolve, reject) => {
                GetTopTag()
                    .then(async res => {
                        // 设置 vuex/localStorage 用户信息
                        commit('SetHotTag', { hotTag: res.data })
                        resolve()
                    }).catch(err => {
                    console.log('err: ', err)
                    reject(err)
                })
            })
        },

        /**
         * @description 获取热门文章
         * @author Ryui
         */
        InitHotArticle ({ commit }) {
            return new Promise((resolve, reject) => {
                GetHotArticle()
                    .then(async res => {
                        // 设置 vuex/localStorage 用户信息
                        commit('SetHotArticle', { hotArticle: res.data })
                        resolve()
                    }).catch(err => {
                    console.log('err: ', err)
                    reject(err)
                })
            })
        },
    }
}
